//d.ui.mouse
/*
 * jQuery Dialog 0.1
 * http://www.bsweb.com.cn
 * 需要库文件
 * 		jquery-ui.js
 * 		buss.toolbar.js
 * 		buss.panel.js
 * 		buss.button.js
 */
(function($,undefined){
	$.widget("ui.dialog",$.ui.panel,{
		options:{		
			header:true,
			closable:true,			
			modal:true,
			draggable:true,
			opacity:0.5,
			isDialog:true,
			cls:'buss_overlay',
			position:{
				of:'body',
				my:'center',
				at:'center',
				collision:'fit',
				using: function(pos) {					
					var topOffset = $(this).css(pos).offset().top;
					if (topOffset < 0) {
						$(this).css('top', pos.top - topOffset);
					}
				}
			},	
			buttonAlign:"center",
			buttonGap:5,
			buttons:null
		},
		_init:function(){			
			this._initPanel();	
			var _self = this;
			var panelTemplate = this.panelTemplate;	
			var maxZIndex = $.maxZIndex();	
			if(this.options.modal){				 
				this.overlay = $.fn.overlay({
										zIndex:++maxZIndex,
										opacity:this.options.opacity,
										cls:this.options.cls
									});			
				
			}			//buss-panel-body
			
			if (this.options.buttons != null) {
				this.panelBodyTemplate.append("<div class='buttonPanel'/>")
				var buttonPanelChild = $("<div style='margin-top:3px;'/>");
				this.panelBodyTemplate.find(".buttonPanel").append(buttonPanelChild);
				 if(this.options.buttonAlign == "left"){
					buttonPanelChild.css("margin-left","20px");
				}else if(this.options.buttonAlign == "right"){
					buttonPanelChild.css("margin-right","20px");
				}				
				//buttonPanelChild.css();
				buttonPanelChild.css({'line-height':'30px','text-align':this.options.buttonAlign});
				for(var i = 0 ;i < this.options.buttons.length; i++){
					var btn = $("<a>").button(this.options.buttons[i]);
					if (i > 0) {
						btn.css('margin-left', this.options.buttonGap + "px");
					}
					buttonPanelChild.append(btn);
				}

				var height = this.panelTemplate.height();
				panelTemplate.css("border-bottom","1px solid #787878");
				panelTemplate.height(height + this.panelBodyTemplate.find('.buttonPanel').height());
			}
			panelTemplate.css({
				position: 'absolute',
				zIndex:++maxZIndex
			})
			panelTemplate.position(this.options.position);
			if (this.options.draggable == true) {
				panelTemplate.draggable({
					handle: ".buss-panel-header"
				});
			}
			panelTemplate.find(".buss-panel-header").bind("mousedown.dialog",function(e){
				var zIndex = parseInt(panelTemplate.css('z-index'));
				zIndex = isNaN(zIndex) ? 0 : zIndex;
				var maxZIndex = $.maxZIndex();
				if (maxZIndex == 0 || maxZIndex > zIndex){
					panelTemplate.css("z-index",maxZIndex + 1);
				}
			})
		},
		close:function(target){
			this._close(target);
			if(this.options.modal == true){
				this.overlay.minimize();
			}
		},
		destroy:function(target){
			//alert("tt");
			this._destroy(target);
			if(this.options.modal == true){
				this.overlay.destroy();
			}
		},
		minimize:function(target){
			this._minimize();
			if(this.options.modal == true){
				this.overlay.minimize();
			}
		},
		restore:function(target){			
			if(this.options.modal == true){
				this.overlay.restore();
			}
			this._restore(target);
		}
	});
	$.extend({
        alert:function(title,message,width,height,type,fn){
            var imgCls = "infoimg";
            if(type === "warning"){
                imgCls = "warningimg";
            }else if(type === "success"){
                imgCls = "successimg";
            }else if(type === "error"){
                imgCls = "errorimg";
            }else{
                imgCls = "infoimg";
			
            }
            var html = "<div class='alert "+imgCls+"'>"+message+"</div>";
            $(html).dialog({
                title:title,
                width:width,
                height:height,
                buttons:[{
                	text:"确 定",
                	type:'button',
                	plain:false,
                	handler:function(){
                		if($.isFunction(fn)){
                            fn();
                        }
                        $(this).dialog("destroy");
                	}
                }]
            });
        },
        confirm:function(title,message,width,height,fn1,fn2){
            var html = "<div class='alert infoimg'>"+message+"</div>";
            $(html).dialog({
                title:title,
                width:width,
                height:height,
                buttons:[
                     {text:'确 定',type:'button',plain:false,handler:function(){
	                    	 if($.isFunction(fn1)){
	                             fn1();
	                         }
	                         $(this).dialog("destroy");
                     }},
                     {text:'取 消',type:'button',plain:false,handler:function(){
                    	 if($.isFunction(fn2)){
                             fn2();
                         }
                         $(this).dialog("destroy");
                     }}
                ]
         })
        }
	})
	
})(jQuery)
